﻿Imports DTO
Imports BUS
Public Class MH_ThemDiem
    Public DiemMonHoc As New DiemMonHocDTO
    Public MaHocSinh As Integer
    Public MaLop As Integer
    Private Sub MH_ThemDiem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim busHK As New HocKyBUS
        cbHocKy.DataSource = busHK.LayBang()
        cbHocKy.DisplayMember = "HocKy"
        cbHocKy.ValueMember = "MaHocKy"
        Dim monHK As New MonHocBUS
        cbMon.DataSource = monHK.LayBang
        cbMon.DisplayMember = "TenMon"
        cbMon.ValueMember = "MaMon"

    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        'Dim oldForm As New MH_CapNhatDiemCaNhan
        DiemMonHoc.HocKy = cbHocKy.SelectedValue
        DiemMonHoc.MaMon = cbMon.SelectedValue
        Try
            DiemMonHoc.MaLop = MaLop
        Catch ex As Exception
            MessageBox.Show("Không lấy dc điểm từ form cũ")
        End Try
        Dim tempBUS2 As New BangDiemBUS
        Dim KiemTraBangDiemDaTonTai As Integer = tempBUS2.KiemTra(MaHocSinh, DiemMonHoc.HocKy)
        If (KiemTraBangDiemDaTonTai = 0) Then
            Dim tempBUS As New DiemMonHocBUS
            Dim kq = tempBUS.KiemTra(DiemMonHoc)
            Dim bdDTO As New BangDiemDTO
            'neu da ton tai
            'chi insert vao bang chitietdiem
            If (kq > 0) Then

                Dim MaDiemMonHoc = tempBUS.LayMaDiemMon(DiemMonHoc)
                bdDTO.MaChiTietDiem = MaDiemMonHoc
                bdDTO.Diem15P = txtDiem15P.Text
                bdDTO.Diem1T = txtDiem1T.Text
                bdDTO.DiemHK = txtDiemHocKy.Text
                bdDTO.MaHocSinh = MaHocSinh
                Dim bdBUS As New BangDiemBUS
                bdBUS.ThemBangDiem(bdDTO)

            Else

                Dim diemmonhocBUS As New DiemMonHocBUS
                diemmonhocBUS.ThemMaDiemMon(DiemMonHoc)

                Dim MaDiemMonHoc = tempBUS.LayMaDiemMon(DiemMonHoc)
                bdDTO.MaChiTietDiem = MaDiemMonHoc
                bdDTO.Diem15P = txtDiem15P.Text
                bdDTO.Diem1T = txtDiem1T.Text
                bdDTO.DiemHK = txtDiemHocKy.Text
                bdDTO.MaHocSinh = MaHocSinh
                Dim bdBUS As New BangDiemBUS
                bdBUS.ThemBangDiem(bdDTO)

            End If
        Else
            MessageBox.Show("Đã có điểm này trong CSDL, vui lòng sử dụng chức năng Cập nhật điểm")
        End If
        
    End Sub
End Class